﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrimaryAlgorithm.IsValid
{
    class MainProject
    {
        static void Main(string[] args)
        {
            Console.WriteLine(IsValid("))"));
            Console.ReadKey();
        }

        /// <summary>
        /// 思路：栈
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsValid(string s)
        {
            Stack<char> sta = new Stack<char>();
            if (s[0] == '{' || s[0] == '(' || s[0] == '[')
                sta.Push(s[0]);
            else
                return false;
            for (int i = 1; i < s.Length; i++)
            {
                if (s[i] == '{' || s[i] == '(' || s[i] == '[')
                {
                    sta.Push(s[i]);
                }
                else
                {
                    if (sta.Count==0)
                    {
                        return false;
                    }
                    char c = sta.Pop();
                    if (Math.Abs(c - s[i]) >2)
                    {
                        return false;
                    }
                }
            }
            if (sta.Count>0)
            {
                return false;
            }

            return true;
        }
    }
}
